<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  
  <title>file | H5教案</title>

  
  <meta name="author" content="John Doe">
  

  
  <meta name="description" content="7、file引入1var fs=require(&amp;quot;fs&amp;quot;);
fs.mkdir创建文件夹
123fs.mkdir(&amp;#x27;./logs&amp;#x27;, (err) =&amp;gt; &amp;#123;  console.log(&amp;#x27;done.&amp;#x27;)&amp;#125;)
fs.rm">
  

  
  
  <meta name="keywords" content="">
  

  <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <meta property="og:title" content="file"/>

  <meta property="og:site_name" content="H5教案"/>

  
  <meta property="og:image" content="/ericxie-book/favicon.ico"/>
  

  <link href="/ericxie-book/favicon.ico" rel="icon">
  <link rel="alternate" href="/ericxie-book/atom.xml" title="H5教案" type="application/atom+xml">
  <link rel="stylesheet" href="/ericxie-book/css/style.css" media="screen" type="text/css">
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
<div class="blog">
  <div class="content">

    <header>
  <div class="site-branding">
    <h1 class="site-title">
      <a href="/ericxie-book/">H5教案</a>
    </h1>
    <p class="site-description"></p>
  </div>
  <nav class="site-navigation">
    <ul>
      
    </ul>
  </nav>
</header>

    <main class="site-main posts-loop">
    <article>

  
    
    <h3 class="article-title"><span>file</span></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/26/file/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-26T07:13:24.000Z">
          2021-01-26
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="7、file"><a href="#7、file" class="headerlink" title="7、file"></a>7、file</h2><h3 id="引入"><a href="#引入" class="headerlink" title="引入"></a>引入</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">var fs=require(<span class="string">&quot;fs&quot;</span>);</span><br></pre></td></tr></table></figure>
<h4 id="fs-mkdir"><a href="#fs-mkdir" class="headerlink" title="fs.mkdir"></a>fs.mkdir</h4><p>创建文件夹</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">fs.mkdir(<span class="string">&#x27;./logs&#x27;</span>, (err) =&gt; &#123;</span><br><span class="line">  console.log(<span class="string">&#x27;done.&#x27;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="fs-rmdir"><a href="#fs-rmdir" class="headerlink" title="fs.rmdir"></a>fs.rmdir</h4><p>删除文件夹</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">fs.rmdir(<span class="string">&#x27;./log&#x27;</span>, () =&gt; &#123;</span><br><span class="line">  console.log(<span class="string">&#x27;done.&#x27;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="fs-existsSync"><a href="#fs-existsSync" class="headerlink" title="fs.existsSync"></a>fs.existsSync</h4><p>判断路径是否存在 同步方法</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (fs.existsSync(<span class="string">&#x27;path&#x27;</span>)) &#123;</span><br><span class="line">  console.log(<span class="string">&#x27;该路径已存在&#x27;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="fs-readFile"><a href="#fs-readFile" class="headerlink" title="fs.readFile"></a>fs.readFile</h4><p>异步读取文件.注意地址中会有转义字符,所以\使用的时候要多敲一个</p>
<p>异步读取方法1</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var fs=require(<span class="string">&quot;fs&quot;</span>);</span><br><span class="line">fs.readFile(<span class="string">&quot;E:\\test\\test10\\server\\config.json&quot;</span>,<span class="string">&quot;utf8&quot;</span>,<span class="keyword">function</span>(err,data)&#123;</span><br><span class="line">    console.log(err);</span><br><span class="line">    console.log(data.toString());</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>异步读取方法2</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">fs.readFile(<span class="string">&#x27;./logs/log-0.txt&#x27;</span>, <span class="string">&#x27;utf-8&#x27;</span>).<span class="keyword">then</span>(result =&gt; &#123;</span><br><span class="line">  console.log(result)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>异步读取方法3</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="function"><span class="title">getFile</span></span>() &#123;</span><br><span class="line">  <span class="built_in">return</span> new Promise((resolve) =&gt; &#123;</span><br><span class="line">    fs.readFile(<span class="string">&#x27;./logs/log-0.txt&#x27;</span>, <span class="string">&#x27;utf-8&#x27;</span>, (err, data) =&gt; &#123;</span><br><span class="line">      resolve(data)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">;(async () =&gt; &#123;</span><br><span class="line">  console.log(await getFile())</span><br><span class="line">&#125;)()</span><br></pre></td></tr></table></figure>
<p>异步读取方法4</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">const fsP = require(<span class="string">&#x27;fs&#x27;</span>).promises</span><br><span class="line">const fsp = fsP.readFile(<span class="string">&#x27;./logs/log-1.txt&#x27;</span>, <span class="string">&#x27;utf-8&#x27;</span>).<span class="keyword">then</span>((result) =&gt; &#123;</span><br><span class="line">  console.log(result)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">console.log(fsP)</span><br></pre></td></tr></table></figure>
<h4 id="fs-readFileSync"><a href="#fs-readFileSync" class="headerlink" title="fs.readFileSync"></a>fs.readFileSync</h4><p>同步读取文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var fs=require(<span class="string">&quot;fs&quot;</span>);</span><br><span class="line">var data=fs.readFileSync(<span class="string">&quot;E:\\test\\test10\\server\\config.json&quot;</span>,<span class="string">&quot;utf8&quot;</span>);</span><br><span class="line">console.log(data.toString());</span><br></pre></td></tr></table></figure>
<h4 id="fs-createReadStream"><a href="#fs-createReadStream" class="headerlink" title="fs.createReadStream"></a>fs.createReadStream</h4><p>读取流模式</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">var fs=require(<span class="string">&quot;fs&quot;</span>);</span><br><span class="line">var stream=fs.createReadStream(<span class="string">&quot;E:\\test\\test10\\server\\config.json&quot;</span>);</span><br><span class="line">var data=<span class="string">&quot;&quot;</span>;</span><br><span class="line">stream.on(<span class="string">&quot;data&quot;</span>,<span class="keyword">function</span>(_data)&#123;</span><br><span class="line">    data+=_data;</span><br><span class="line">&#125;);</span><br><span class="line">stream.on(<span class="string">&quot;end&quot;</span>,<span class="function"><span class="title">function</span></span>()&#123;</span><br><span class="line">    console.log(data);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="fs-readline"><a href="#fs-readline" class="headerlink" title="fs.readline"></a>fs.readline</h4><p>逐行读取文本内容，这里需要引入一个逐行读取的模块</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">var readline=require(<span class="string">&quot;readline&quot;</span>);</span><br></pre></td></tr></table></figure>
<p>readline.createInterface会将读取到的文件流存在一个读取列表中，通过line事件读入每行的内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">var fs=require(<span class="string">&quot;fs&quot;</span>);</span><br><span class="line">var readline=require(<span class="string">&quot;readline&quot;</span>);</span><br><span class="line">var rl=readline.createInterface(fs.createReadStream(<span class="string">&quot;./server/data/a.txt&quot;</span>));</span><br><span class="line">rl.on(<span class="string">&quot;line&quot;</span>,<span class="keyword">function</span>(line)&#123;</span><br><span class="line">    console.log(line);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="fs-stat"><a href="#fs-stat" class="headerlink" title="fs.stat"></a>fs.stat</h4><p>读取目录和文件信息</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">fs.readdir(<span class="string">&#x27;./&#x27;</span>, (err, data) =&gt; &#123;</span><br><span class="line">  data.forEach((value, index) =&gt; &#123;</span><br><span class="line">    fs.stat(`./<span class="variable">$&#123;value&#125;</span>`, (err, stats) =&gt; &#123;</span><br><span class="line">      // console.log(value + <span class="string">&#x27;:&#x27;</span> + stats.size)</span><br><span class="line">      console.log(value + <span class="string">&#x27; is &#x27;</span> + (stats.isDirectory() ? <span class="string">&#x27;directory&#x27;</span> : <span class="string">&#x27;file&#x27;</span>))</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>获取当前指定文件夹下所有的文件和文件夹</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">var fs=require(<span class="string">&quot;fs&quot;</span>);</span><br><span class="line">getDir(<span class="string">&quot;./&quot;</span>)</span><br><span class="line"><span class="keyword">function</span> getDir(path)&#123;</span><br><span class="line">    fs.readdir(path, (err, data) =&gt; &#123;</span><br><span class="line">        data.forEach((value, index) =&gt; &#123;</span><br><span class="line">          fs.stat(`<span class="variable">$&#123;path+value&#125;</span>`, (err, stats) =&gt; &#123;</span><br><span class="line">             <span class="keyword">if</span>(!stats)<span class="built_in">return</span>;</span><br><span class="line">            // console.log(value + <span class="string">&#x27;:&#x27;</span> + stats.size)</span><br><span class="line">            console.log(value + <span class="string">&#x27; is &#x27;</span> + (stats.isDirectory() ? <span class="string">&#x27;directory&#x27;</span> : <span class="string">&#x27;file&#x27;</span>))</span><br><span class="line">            <span class="keyword">if</span>(stats.isDirectory())&#123;</span><br><span class="line">                getDir(path+value+<span class="string">&quot;/&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="fs-statSync"><a href="#fs-statSync" class="headerlink" title="fs.statSync"></a>fs.statSync</h4><p>对象提供有关文件的信息</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var stats=fs.statSync(path);</span><br><span class="line">stats.isFile() //判断当前路径的是不是文件</span><br></pre></td></tr></table></figure>
<h4 id="fs-readdir"><a href="#fs-readdir" class="headerlink" title="fs.readdir"></a>fs.readdir</h4><p>异步读取文件的内容.回调有两个参数 (err, files)，其中 files 是目录中的文件名的数组（不包括 ‘.’ 和 ‘..’）。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">fs.readdir(path,<span class="keyword">function</span>(error,files)&#123;</span><br><span class="line">    //files是一个数组,里面包含了当前路径下所有的文件和文件夹名称</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="fs-readdirSync"><a href="#fs-readdirSync" class="headerlink" title="fs.readdirSync"></a>fs.readdirSync</h4><p>同步读取路径当中的内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">var files=fs.readdirSync(path);</span><br></pre></td></tr></table></figure>
<h4 id="fs-writeFile"><a href="#fs-writeFile" class="headerlink" title="fs.writeFile"></a>fs.writeFile</h4><p>异步地将数据写入到一个文件，如果文件已存在则覆盖该文件。 data 可以是字符串或 buffer。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">fs.writeFile(<span class="string">&#x27;a.txt&#x27;</span>, <span class="string">&#x27;文本内容&#x27;</span>, <span class="string">&#x27;utf8&#x27;</span>, <span class="function"><span class="title">function</span></span>()&#123;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h4 id="fs-writeFileSync"><a href="#fs-writeFileSync" class="headerlink" title="fs.writeFileSync"></a>fs.writeFileSync</h4><p>异步写入文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fs.writeFileSync(<span class="string">&#x27;c:\\demo\a.txt&#x27;</span>, <span class="string">&quot;文本内容&quot;</span>);</span><br></pre></td></tr></table></figure>
<h4 id="fs-appendFile"><a href="#fs-appendFile" class="headerlink" title="fs.appendFile"></a>fs.appendFile</h4><p>向文件尾部增加新数据,如果该文件不存在就会创建一个新文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">fs.appendFile(<span class="string">&quot;./server/data/a.txt&quot;</span>,<span class="string">&quot;新增的内容&quot;</span>,<span class="keyword">function</span>(error)&#123;</span><br><span class="line">    console.log(error);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="fs-appendFileSync"><a href="#fs-appendFileSync" class="headerlink" title="fs.appendFileSync"></a>fs.appendFileSync</h4><p>同步向文件尾部增加新内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">try&#123;</span><br><span class="line">    fs.appendFileSync(<span class="string">&quot;./server/data/a.txt&quot;</span>,<span class="string">&quot;新增内容&quot;</span>);</span><br><span class="line">&#125;catch(e)&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="fs-rename"><a href="#fs-rename" class="headerlink" title="fs.rename"></a>fs.rename</h4><p>异步地把 oldPath 文件重命名为 newPath 提供的路径名。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">fs.rename(<span class="string">&#x27;旧文件.txt&#x27;</span>, <span class="string">&#x27;新文件.txt&#x27;</span>, (err) =&gt; &#123;</span><br><span class="line">  <span class="keyword">if</span> (err) throw err;</span><br><span class="line">  console.log(<span class="string">&#x27;重命名完成&#x27;</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="fs-unlink"><a href="#fs-unlink" class="headerlink" title="fs.unlink"></a>fs.unlink</h3><p>删除文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">fs.unlink(<span class="string">&#x27;./logs/log1.txt&#x27;</span>, (err) =&gt; &#123;</span><br><span class="line">  console.log(<span class="string">&#x27;done.&#x27;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="监视文件"><a href="#监视文件" class="headerlink" title="监视文件"></a>监视文件</h4><p>监视文件的变化</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">fs.watch(<span class="string">&#x27;./logs/log-0.txt&#x27;</span>, () =&gt; &#123;</span><br><span class="line">  console.log(0)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="fs-createWriteStream"><a href="#fs-createWriteStream" class="headerlink" title="fs.createWriteStream"></a>fs.createWriteStream</h4><p>流式写入,如果是大文件,内容较多,例如视频等就需要流式写入,防止内存卡死</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">var streamWriter = fs.createWriteStream(<span class="string">&#x27;c:\\demo\a.txt&#x27;</span>);</span><br><span class="line">setInterval(<span class="function"><span class="title">function</span></span>()&#123;</span><br><span class="line">    streamWriter.write(`<span class="variable">$&#123;new Date&#125;</span>\n`, (error) =&gt; &#123;</span><br><span class="line">    console.log(error);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;,1000)</span><br></pre></td></tr></table></figure>
<h4 id="fs-createReadStream-1"><a href="#fs-createReadStream-1" class="headerlink" title="fs.createReadStream()"></a>fs.createReadStream()</h4><p>读取文件流</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">const fs = require(<span class="string">&#x27;fs&#x27;</span>)</span><br><span class="line">const readstream = fs.createReadStream(<span class="string">&#x27;./note.txt&#x27;</span>)</span><br><span class="line">const writestream = fs.createWriteStream(<span class="string">&#x27;./note2.txt&#x27;</span>)</span><br><span class="line">writestream.write(readstream)</span><br></pre></td></tr></table></figure>
<h4 id="zlib"><a href="#zlib" class="headerlink" title="zlib"></a>zlib</h4><p>压缩</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">const fs = require(<span class="string">&#x27;fs&#x27;</span>)</span><br><span class="line">const zlib = require(<span class="string">&#x27;zlib&#x27;</span>)</span><br><span class="line"></span><br><span class="line">const gzip = zlib.createGzip()</span><br><span class="line">const readstream = fs.createReadStream(<span class="string">&#x27;./note.txt&#x27;</span>)</span><br><span class="line">const writestream = fs.createWriteStream(<span class="string">&#x27;./note2.txt&#x27;</span>)</span><br><span class="line">readstream</span><br><span class="line">  .pipe(gzip)</span><br><span class="line">  .pipe(writestream)</span><br><span class="line">writestream.write(readstream)</span><br></pre></td></tr></table></figure>
<h4 id="解析xlsx文件，Excel表格"><a href="#解析xlsx文件，Excel表格" class="headerlink" title="解析xlsx文件，Excel表格"></a>解析xlsx文件，Excel表格</h4><p>需要先下一个第三方模块node-xlsx,这里完成了一个excel表格筛选重建的内容然后创建一个新的表格文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">fs.readFile(<span class="string">&quot;G:\\abc.xlsx&quot;</span>,<span class="keyword">function</span>(error,data)&#123;</span><br><span class="line">   data=xlsx.parse(data);</span><br><span class="line">   var arr=data[0].data.slice(0,2);</span><br><span class="line">   data[0].data.forEach(item=&gt;&#123;</span><br><span class="line">       <span class="keyword">if</span>(String(item[0]).includes(<span class="string">&quot;刘&quot;</span>)) arr.push(item);</span><br><span class="line">   &#125;);</span><br><span class="line">   var buffer = xlsx.build([&#123;name: <span class="string">&quot;cde&quot;</span>, data: arr&#125;]);</span><br><span class="line">   fs.writeFile(<span class="string">&quot;G:\\cde.xlsx&quot;</span>,buffer,<span class="string">&quot;utf-8&quot;</span>,<span class="function"><span class="title">function</span></span>()&#123;</span><br><span class="line">       console.log(<span class="string">&quot;完成&quot;</span>);</span><br><span class="line">   &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="通过ajax上传图片并且保存在服务器"><a href="#通过ajax上传图片并且保存在服务器" class="headerlink" title="通过ajax上传图片并且保存在服务器"></a>通过ajax上传图片并且保存在服务器</h4><p>前端文件<br>注意var reader = new FileReader();这里是新建了一个文件渲染实例用来异步读取这个文件数据<br>reader.readAsArrayBuffer读取对应的文件<br>读取到的文件将会被放在reader.result中</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=<span class="string">&quot;en&quot;</span>&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset=<span class="string">&quot;UTF-8&quot;</span>&gt;</span><br><span class="line">    &lt;meta name=<span class="string">&quot;viewport&quot;</span> content=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span><br><span class="line">    &lt;title&gt;Document&lt;/title&gt;</span><br><span class="line"></span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;input <span class="built_in">type</span>=<span class="string">&quot;file&quot;</span>&gt;</span><br><span class="line">    &lt;script&gt;</span><br><span class="line">        var file=document.querySelector(<span class="string">&quot;[type=file]&quot;</span>);</span><br><span class="line">        file.addEventListener(<span class="string">&quot;change&quot;</span>,sendFiles);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">function</span> <span class="function"><span class="title">sendFiles</span></span>() &#123;</span><br><span class="line">            var reader = new FileReader();</span><br><span class="line">            reader.readAsArrayBuffer(file.files[0]);</span><br><span class="line">            reader.addEventListener(<span class="string">&quot;load&quot;</span>,e=&gt;ajax(reader.result,file.files[0].name));</span><br><span class="line">         &#125;</span><br><span class="line"></span><br><span class="line">         <span class="keyword">function</span> ajax(data,name)&#123;</span><br><span class="line">            var xhr=new XMLHttpRequest();</span><br><span class="line">            xhr.addEventListener(<span class="string">&quot;load&quot;</span>,loadHandler);</span><br><span class="line">            xhr.open(<span class="string">&quot;POST&quot;</span>,<span class="string">&quot;http://localhost:4001/&quot;</span>+name);</span><br><span class="line">            xhr.send(data);</span><br><span class="line">         &#125;</span><br><span class="line">         <span class="keyword">function</span> loadHandler(e)&#123;</span><br><span class="line"></span><br><span class="line">         &#125;</span><br><span class="line">    &lt;/script&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
<p>服务端文件</p>
<p>req.pipe是管道，将前端发送过来的数据通过管道发送存储文件流</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">var http=require(<span class="string">&quot;http&quot;</span>);</span><br><span class="line">var fs=require(<span class="string">&quot;fs&quot;</span>);</span><br><span class="line">http.createServer((req,res)=&gt;&#123;</span><br><span class="line">    res.writeHead(200,&#123;</span><br><span class="line">        <span class="string">&quot;content-type&quot;</span>:<span class="string">&quot;text/html;charset=utf-8&quot;</span>,</span><br><span class="line">        <span class="string">&quot;Access-Control-Allow-Origin&quot;</span>:<span class="string">&quot;*&quot;</span></span><br><span class="line">    &#125;);</span><br><span class="line">    req.pipe(fs.createWriteStream(<span class="string">&quot;./&quot;</span>+req.url,<span class="string">&quot;base64&quot;</span>));</span><br><span class="line">    res.end(<span class="string">&quot;完成了&quot;</span>);       </span><br><span class="line">&#125;).listen(4001);</span><br></pre></td></tr></table></figure>
<h4 id="使用formData数据上传多张图片"><a href="#使用formData数据上传多张图片" class="headerlink" title="使用formData数据上传多张图片"></a>使用formData数据上传多张图片</h4><p>客户端</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=<span class="string">&quot;en&quot;</span>&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset=<span class="string">&quot;UTF-8&quot;</span>&gt;</span><br><span class="line">    &lt;meta name=<span class="string">&quot;viewport&quot;</span> content=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span><br><span class="line">    &lt;title&gt;Document&lt;/title&gt;</span><br><span class="line"></span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">  &lt;form action=<span class="string">&quot;#&quot;</span>&gt;</span><br><span class="line">      &lt;input <span class="built_in">type</span>=<span class="string">&quot;text&quot;</span> name=<span class="string">&quot;user&quot;</span>&gt;</span><br><span class="line">    &lt;input <span class="built_in">type</span>=<span class="string">&quot;file&quot;</span> name=<span class="string">&quot;imgs&quot;</span> multiple&gt;</span><br><span class="line">    &lt;button <span class="built_in">type</span>=<span class="string">&quot;submit&quot;</span>&gt;提交&lt;/button&gt;</span><br><span class="line">  &lt;/form&gt;</span><br><span class="line">    &lt;script&gt;</span><br><span class="line">        var form=document.querySelector(<span class="string">&quot;form&quot;</span>);</span><br><span class="line">        form.addEventListener(<span class="string">&quot;submit&quot;</span>,sendFiles);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">function</span> sendFiles(e) &#123;</span><br><span class="line">            e.preventDefault();</span><br><span class="line">            var data=new FormData(form);</span><br><span class="line">            ajax(data)</span><br><span class="line">         &#125;</span><br><span class="line"></span><br><span class="line">         <span class="keyword">function</span> ajax(data)&#123;</span><br><span class="line">            var xhr=new XMLHttpRequest();</span><br><span class="line">            xhr.addEventListener(<span class="string">&quot;load&quot;</span>,loadHandler);</span><br><span class="line">            xhr.open(<span class="string">&quot;POST&quot;</span>,<span class="string">&quot;http://localhost:4001/&quot;</span>);</span><br><span class="line">            // xhr.setRequestHeader(<span class="string">&quot;content-type&quot;</span>,<span class="string">&quot;multipart/form-data&quot;</span>);</span><br><span class="line">            xhr.send(data);</span><br><span class="line">         &#125;</span><br><span class="line">         <span class="keyword">function</span> loadHandler(e)&#123;</span><br><span class="line"></span><br><span class="line">         &#125;</span><br><span class="line">    &lt;/script&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
<p>服务端<br>multiparty插件是接收和解析formData表单数据的插件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">var http=require(<span class="string">&quot;http&quot;</span>);</span><br><span class="line">var fs=require(<span class="string">&quot;fs&quot;</span>);</span><br><span class="line">var multiparty=require(<span class="string">&quot;multiparty&quot;</span>);</span><br><span class="line"></span><br><span class="line">http.createServer((req,res)=&gt;&#123;</span><br><span class="line">    res.writeHead(200,&#123;</span><br><span class="line">        <span class="string">&quot;content-type&quot;</span>:<span class="string">&quot;text/plain;charset=utf-8&quot;</span>,</span><br><span class="line">        <span class="string">&quot;Access-Control-Allow-Origin&quot;</span>:<span class="string">&quot;*&quot;</span></span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="built_in">let</span> form=new multiparty.Form(&#123;uploadDir:<span class="string">&quot;./&quot;</span>&#125;);</span><br><span class="line">    form.parse(req,<span class="function"><span class="title">function</span></span>()&#123;</span><br><span class="line">        res.writeHead(200,&#123;<span class="string">&quot;content-type&quot;</span>:<span class="string">&quot;text/plain&quot;</span>&#125;);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    form.on(<span class="string">&quot;file&quot;</span>,<span class="keyword">function</span>(name,file)&#123;</span><br><span class="line">       fs.rename(file.path,file.originalFilename,<span class="keyword">function</span>(error)&#123;</span><br><span class="line"></span><br><span class="line">       &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">    res.end();</span><br><span class="line">&#125;).listen(4001);</span><br></pre></td></tr></table></figure>
      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>

  






    </main>

    <footer class="site-footer">
  <p class="site-info">
    Proudly powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and
    Theme by <a href="https://github.com/CodeDaraW/Hacker" target="_blank">Hacker</a>
    </br>
    
    &copy; 2021 John Doe
    
  </p>
</footer>
    
    
  </div>
</div>
</body>
</html>